package com.li.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.mapper.SysUserMapper;
import com.li.service.SysMenuService;
import com.li.service.SysUserService;
import com.li.system.SysUser;
import com.li.vo.RouterVo;
import com.li.vo.SysUserQueryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xiaoli
 * @since 2022-10-29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService sysMenuService;

    //用户列表分页查询
    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, @Param("vo") SysUserQueryVo sysUserQueryVo) {

        return baseMapper.selectPage(pageParam, sysUserQueryVo);
    }

    //更改用户状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据用户Id查询
        SysUser sysUser = baseMapper.selectById(id);
        //设置修改状态
        sysUser.setStatus(status);
        //调用方法修改
        baseMapper.updateById(sysUser);
    }

    @Override
    public SysUser selectByUserName(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        //根据username查询用户基本信息
        SysUser sysUser = this.selectByUserName(username);
        //根据userId查询菜单权限
        List<RouterVo> routerVoList = sysMenuService.getUserMenuList(sysUser.getId());
        //根据userId查询按钮权限
        List<String> permsList = sysMenuService.getUserButtonList(sysUser.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("name",username);
        map.put("avatar","https://img.zcool.cn/community/01d7225b0e7eeca8012181b0e12a56.gif");
        map.put("roles","[\"admin\"]");
        //菜单权限
        map.put("routers",routerVoList);
        //按钮权限
        map.put("buttons",permsList);
        return map;
    }
}
